<template>
	<view class="otherStudents">
		<u-navbar title="切换其他学生" :autoBack="true" :placeholder="true">
		</u-navbar>

		<view class="imgbox">
			<view class="studentName">
				当前学生：{{userInfo.name}}
			</view>
			<image src="https://mp.blrwworkshop.com/imgs/student.png" mode=""></image>
		</view>

		<view class="selectBox">
			<view class="titleBox">
				请选择学生
			</view>

			<view class="studentList">
				<view class="studentItem" v-for="(item,index) in studentList" :key="index"
					@click="handleSelectstudent(item,index)">
					<image v-if="studentId ==item.id"
						:src="role ==1 ?'https://mp.blrwworkshop.com/imgs/checksed.png' :'https://mp.blrwworkshop.com/imgs/checkss.png'"
						mode=""></image>
					<image v-else src="https://mp.blrwworkshop.com/imgs/check.png" mode=""></image>
					<view class="">
						{{item.name}}
					</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		qryBindStudents,
		getStudentInfo,
		changeStudent,
		changeCurrentStudent
	} from "@/api/mine.js"
	export default {
		data() {
			return {
				role: uni.getStorageSync('role'),
				studentList: [{
						name: "学生姓名1"
					},
					{
						name: "学生姓名2"
					},
				],
				studentId: 0,
				userInfo: {},
			}
		},
		onLoad() {
			this.handleGetStudentInfo()
			this.handleGetqryBindStudents()
		},
		methods: {
			//获取学生信息
			async handleGetStudentInfo() {
				let res = await getStudentInfo()
				if (res.code == 200) {
					this.userInfo = res.student
					this.studentId = res.student.id
				}
			},
			async handleGetqryBindStudents() {
				let res = await qryBindStudents()
				if (res.code == 200) {
					this.studentList = res.data
				}
			},
			async handleSelectstudent(item, index) {
				uni.showLoading({
					title: "正在切换",
					mask: true
				});
				if (this.role == 1) {
					let res = await changeStudent({
						studentId: item.id
					})
					if (res.code == 200) {
						setTimeout(() => {
							uni.hideLoading()
							this.handleGetStudentInfo()
							this.handleGetqryBindStudents()
							uni.showToast({
								title: '切换成功',
								icon: 'none'
							})
							uni.$emit("changeRole", true)
						}, 1000)

					} else {
						uni.hideLoading()
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}
				if (this.role == 2) {
					let res = await changeCurrentStudent({
						studentId: item.id
					})
					if (res.code == 200) {
						setTimeout(() => {
							uni.hideLoading()
							this.handleGetStudentInfo()
							this.handleGetqryBindStudents()
							uni.showToast({
								title: '切换成功',
								icon: 'none'
							})
							uni.$emit("changeRole", true)
						}, 1000)

					} else {
						uni.hideLoading()
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}


			},
		}
	}
</script>

<style scoped lang="scss">
	.otherStudents {
		width: 100%;
		min-height: 100vh;
		display: flex;
		flex-direction: column;
		align-items: center;

		.imgbox {
			width: 580rpx;
			height: 580rpx;
			margin-top: 38rpx;

			.studentName {
				font-weight: 600;
				font-size: 36rpx;
				color: #333333;
				text-align: center;
			}

			image {
				width: 580rpx;
				height: 580rpx;
			}
		}

		.selectBox {
			width: 580rpx;
			margin-top: 58rpx;

			.titleBox {
				font-weight: 600;
				font-size: 36rpx;
				color: #333333;
			}

			.studentList {
				.studentItem {
					display: flex;
					align-items: center;
					font-weight: 600;
					font-size: 32rpx;
					color: #333333;
					margin-top: 44rpx;

					image {
						width: 36rpx;
						height: 36rpx;
						margin-right: 10rpx;
					}
				}
			}

		}

	}
</style>